Mixes up the order of the items in target, in place: afterwards it holds the same items in a random order. It makes no copy. Any kind of item can be shuffled, text included.
list_shuffle(target)| Name | Type |
|---|---|
target | list |
Nothing.
Mixes up the order of the items in target, a list. Afterwards the list holds exactly the same items as before, in a random order. It is the way to shuffle a deck of cards.
list_shuffle(deck);list_shuffle moves the items that are already in target. It does not make a new list, and it needs no extra memory. The old order is gone afterwards. If you want to keep it, make a copy with list_copy first and shuffle the one you want mixed up.
It mixes the items the list holds right now, the number list_length gives you. The empty places up to the capacity are not touched. A list with no items or with one item is left as it is.
Any kind of item can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the list holds. (A sort is different: it only works on numbers and text.)
A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:
roll_reseed changes it. Calling roll_reseed with a number starts the stream again from that number, so roll_reseed(7) just before list_shuffle gives the same shuffle every time you do it with the list in the same order to start with. Use that when you want a shuffle you can repeat, like a puzzle everyone gets the same way. For a different shuffle each game, reseed from something the player decides, such as how long they waited before pressing a button (see roll_reseed).roll_int, roll_real and roll_chance that comes after it. If a game needs the rest of its rolls to stay exactly the same, that is worth knowing.Every possible order can come up. It is very slightly more likely to land on some orders than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.
| You do this | What happens |
|---|---|
| shuffle a list with no items or one item | nothing changes, and no random numbers are used |
| shuffle a list that has the same item more than once | works as normal. Equal items are interchangeable, so you cannot tell which is which |
| shuffle and hope an item moves | it may stay where it was. That can happen with random order, and it gets less likely the more items there are |
| give it an array or a grid | refused when checked, and the message names the function that shuffles that kind, like array_shuffle |
| give it a stack or a queue | refused when checked. Their order is what they are for, so they have no shuffle |
| use it where a value is needed | refused when checked. It gives nothing back |
A script that takes a list as a parameter works on the caller's own list (see Lists). So list_shuffle inside a script shuffles the caller's list.
Nothing is set aside: the items are swapped where they stand, one swap for each item after the first. No timing was measured.
/// @type {list<string>}
/// @capacity 4
var deck = ["ace", "king", "queen", "jack"];
roll_reseed(7); // the same shuffle every time this line runs
list_shuffle(deck); // deck holds the same four cards in a new order
var top = deck[0]; // whichever card landed firstShuffles the list_length(target) items the list holds - never its capacity. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.